<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String strUserId = request.getSession().getAttribute("stxtUName").toString();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/ext-all.css">  
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/edit-grid.css">  
	<script type="text/javascript" src="<%=path%>/js/ext/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/ext-all.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/DWRTreeLoader.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/SearchField.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/query-dwr-proxy.js"></script>	
	<script type="text/javascript" src="<%=path%>/js/ext/ext-lang-zh_CN.js"></script>
	<script type='text/javascript' src='/btsm/dwr/interface/JFolderBeb.js'></script>
	<script type='text/javascript' src='/btsm/dwr/engine.js'></script>
	<script type='text/javascript' src='/btsm/dwr/util.js'></script>	 		
  </head>
  
  <body>
	<div id="westDiv">		
		<div id="folderTree" style="height:80%"/>
		<div id="otherTree"/>
		<div id="comTree"/>
	</div>	
	<div id="contentDiv" style="width:100%;height:100%">
	<form id="downForm" action="<%=path%>/DownLoadServlet">
		<input type="hidden" name="strDownFileID" id="strDownFileID_ID">
	</form>
 
  
<script type="text/javascript">
var contextMenu;//右键菜单 
var folderWindow;
var folderForm; 
var ds;
var fileEditWindow;
var selectFolderId;
Ext.form.Field.prototype.msgTarget = 'side'; 
Ext.onReady(function()
{    		
	var folderTreeDiv = Ext.get("folderTree");	
	var Tree = Ext.tree;    
    var folderRoot = new Tree.AsyncTreeNode({
           text: '单位文档', 
		   icon:'/btsm/js/ext/resources/images/user.gif',
           draggable:false, 
           id:'root'
     });	 
	var treePanel = new Ext.tree.TreePanel( {
		el : "folderTree",
		border : false,
		animate : true,
		autoHeight : true,
		height:folderTreeDiv.getComputedHeight(),
		rootVisible : true,
		autoScroll : true,
		containerScroll : true,
		loader : new Ext.tree.DWRTreeLoader( {
			dwrCall:JFolderBeb.getComFolders,queryParam:{id:'root',strUserID:'<%=strUserId%>',strDepth:1}
		}),
		root : folderRoot
	});
	treePanel.render();
	folderRoot.expand(true,true);	

    var viewport = new Ext.Viewport({
	    layout:'border',
	    frame:true,
	    items:[
	    	{
		        region: 'west',
		        contentEl: 'westDiv',
		        margins:'110 5 30 5',
		        split: true,
		        border: true,
		        collapsible: true,
		        width: 200
		    },
	    	{
		        //title: '文档资料 ',
		        region: 'center',
		        contentEl: 'contentDiv',
		        margins:'110 30 30 5',
		        split: true,
		        border: true	
		    }]
		});
 
	treePanel.on('checkchange', function(node, checked) 
	{      
		if(checked)
		{       
			var chs  = treePanel.getChecked();   
			for(var i = 0; i < chs.length; i++) 
			{   
				if (chs[i].attributes['id'] != node.attributes['id'])
				{   
					chs[i].attributes.checked = false;
					chs[i].ui.toggleCheck(false);    
				}
				
			}
			treePanel.getSelectionModel().select(node);
		}
	});
	
	
	function checkchange(node,checked)
	{
		node.attributes.checked = checked; 
		node.ui.toggleCheck(checked)
		if(checked)
		{
			var chs  = treePanel.getChecked(); 
			for(var i = 0; i < chs.length; i++) 
			{   
				if (chs[i].attributes['id'] != node.attributes['id'])
				{   
					chs[i].attributes.checked = false;
					chs[i].ui.toggleCheck(false);    
				}
			}
		}
		
	}
		
 	
	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect: true});	
	var recordType = Ext.data.Record.create([								
                			   {name: 'id', type: "string"},
                	           {name: 'fileName', type: "string"},
                	           {name: 'uploadTime', mapping: 'uploadTime', type: 'date', dateFormat: 'Y/m/d'},//文件上传时间
                	           {name: 'userId', type: "string"},
                	           {name: 'isShare', type: "string"},
                	           {name: 'shareBeginTime', mapping: 'shareBeginTime', type: 'date', dateFormat: 'Y/m/d'},
                	           {name: 'shareEndTime', mapping: 'shareEndTime', type: 'date', dateFormat: 'Y/m/d'}
                	     ]);

	var cm  = new Ext.grid.ColumnModel(
			[
				sm,
	            {header: "文件名", width: 300, sortable: true, dataIndex: 'fileName',align:'left'},	            
	            {header: "上传时间", width: 80, sortable: true, dataIndex: 'uploadTime',renderer: Ext.util.Format.dateRenderer('Y-m-d'),align:'left'},
	            {header: "上传人", width: 80, sortable: true, dataIndex: 'userId',align:'left'},	
	            {header: "共享开始时间", width: 80, sortable: true, dataIndex: 'shareBeginTime',renderer: Ext.util.Format.dateRenderer('Y-m-d'),align:'left'},
	            {header: "共享结束时间", width: 80, sortable: true, dataIndex: 'shareEndTime',renderer: Ext.util.Format.dateRenderer('Y-m-d'),align:'left'}, 
				{header: "下载", width: 50, sortable: false, dataIndex: 'id',renderer: showDownload}				
			]
		);
	
	
	function showDownload(value, cellmeta, record, rowIndex, columnIndex, store) 
	{   
		var isShare = record.get("isShare");
		var str = "";
		if(isShare =="true")	
			str = "<input type='button' value='下载' class='x-btn button' onclick='downLoadFile(\"" + value  +   "\")'>";      
		return str;  
	} 
	
	function formatDate(date)
	{
		if(date != null)
			return "";
		else
			return "";
	}
	
	ds = new Ext.data.Store({
	        proxy: new Ext.data.DWRProxy(JFolderBeb.getComContents, true),	
	        reader: new Ext.data.ListRangeReader({
	            totalProperty: 'totalSize',
	            id: 'id'
	        }, recordType),
	        sortInfo:{field:'id', direction:'ASC'}// 排序信息
	    });			
	 var contentDiv = Ext.get("contentDiv");
	 var grid = new Ext.grid.GridPanel({
	    	el: 'contentDiv',	    	
	        ds: ds,
	        cm: cm,
			sm:sm,
			title: '文档资料 ',
		    //height:520,
			height: contentDiv.getComputedHeight(), 
	        //width:contentDiv.getComputedWidth(),
	        frame:false,
			border:false,
	        collapsible:true,
			autoExpandColumn:1, 
	        enableDragDrop:false, 
			tbar: [
				'查询: ', ' ',
				new Ext.ux.form.SearchField({
					store: ds,
					width:320
				})],
			bbar: new Ext.PagingToolbar({
			        pageSize: 17,
			        store: ds,
			        displayInfo: true,
			        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			        emptyMsg: "没有记录"
			    })
					
				
	    });
	    
	
	    // render it
	    grid.render();
		var strID = "0";
		ds.on('beforeload', function() {
			  ds.baseParams = {
					id:0,
					start:0,
					limit:17,
					strUserID:'<%=strUserId%>'
			  };
			});		
		
		ds.load({params:{start:0,limit:17, id:0,strUserID:'<%=strUserId%>'}});
		
		
		 treePanel.on('click',function(node)
		 {  
			node.expand();      
			var check = node.attributes.checked
			checkchange(node,!check);
			selectFolderId = node.id;	
			ds.on('beforeload', function() {
				  ds.baseParams = {
						id:node.id, 
						limit:17,
						strUserID:'<%=strUserId%>'
				  };
				});						
			ds.load({params:{start:0,limit:17, id: selectFolderId,strUserID:'<%=strUserId%>'}}); 
			grid.getColumnModel().setHidden(6,false);
			grid.setTitle("单位文档");
				
		}); 
	 
		 
		
	var EditForm = function(){
			this.idTextField = {
					xtype : 'hidden',
					fieldLabel : "strFileID",
					readOnly : true,
					name : "id"
				};
			this.fileNameTextField = {
					xtype : 'textfield',
					fieldLabel : "文件名",
					allowBlank : false,
					name : "fileName",
					width:260,
					blankText :'请输入文件名'
			};
			
			EditForm.superclass.constructor.call(this, {
					bodyStyle : 'padding:5px 5px 0',
					frame : true,
					id: 'EditForm',
					labelAlign: 'right',
					labelWidth:60,
					items : [this.idTextField, 
							 this.fileNameTextField
							 ]
				});	
	}
	Ext.extend(EditForm, Ext.form.FormPanel);	
	var EditWindow = function() {
		var _win = this;
		this.formPanel = new EditForm();
		var _form = this.formPanel.getForm();
		EditWindow.superclass.constructor.call(this, {
			title : '修改文件名',
			width : 400,
			height : 120,
			resizable : true,
			plain : false,
			border : false,
			modal : true,
			autoScroll : false,
			layout : 'fit',
			closeAction : 'hide',
			items : this.formPanel,
			buttonAlign:'center',
			buttons : 
			[
				{	text : ' 保存',
					handler : function() 
					{
						if (_form.isValid()) 
						{
							var form = _form.getValues(false);
							FileBo = {};
							FileBo.fileId =  form.id;	
							FileBo.fileName =  form.fileName;				
							JFolderBeb.editFileName(FileBo,function(data)
							{
								_win.hide();
								if(data)
								{										
									Ext.Msg.alert('成功',"操作成功！",function()
									{
										ds.load({params:{start:0,limit:17,id: selectFolderId,strUserID:'<%=strUserId%>'}});
									}); 
								}else
								{
									Ext.Msg.alert("失败","操作失败!");
								}	
							})
						}
					}
				},{
					text : '取消',
					handler : function() {
						_win.hide();
					},
					tooltip : '关闭对话框'
				}
			]	
		});
	}
	Ext.extend(EditWindow, Ext.Window);
	var fileEditWindow = new EditWindow();	 
	function gridRowDBLClick()
	{	    
		var record = grid.getSelectionModel().getSelected();// 返回值为 Record 类型	             	                
		if(!record){
			Ext.Msg.alert("提示","请选择需要修改的文件!");
			return;
		}
		if (record != null) {
			if(!fileEditWindow)
			{
				fileEditWindow = new EditWindow();				
			}
			fileEditWindow.show();			
			fileEditWindow.formPanel.getForm().loadRecord(record);
		} 
	};	
		
		
		

});	

 
function downLoadFile(strFileID)
{	
	document.getElementById("strDownFileID_ID").value = strFileID;
	document.getElementById("downForm").submit();			
}


  </script>
  
  
 </body>  
</html>
